package db;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.sql.*;

/**
 *
 * @author Admin
 */
public class dbm {

    private String url = "jdbc:postgresql://localhost/bank:5432";
    private String login="bankier";
    private String password="bankier";
  //  private String db="bank";
    protected Connection connect=null;
    public Statement stmt;
    public ResultSet rs;
    public dbm(){
//connect();
}
 public boolean connect()  {
        try  // zaladowanie sterownika
        {
            Class.forName("org.postgresql.Driver"); // zaladowanie sterownika
        }
        catch(java.lang.ClassNotFoundException e)
        {

    //        JOptionPane.showMessageDialog(null, "Blad: Nie znaleziono sterownika ODBC.");

        }

        try  // wykonanie polaczenia
       {
            connect = DriverManager.getConnection(url,login,password); // polaczenie z baza

            stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
           //dmd=connect.getMetaData();
            return true;
       }

       catch(SQLException ex)
       {
          //  JOptionPane.showMessageDialog(null,"Wyjatek SQL - Nie udalo się polączenie z bazą danych.\n"+ex);
            return false;
       }
    }
public void close() {
        try{
           stmt.close();
           connect.close();
        }catch(SQLException ex) {
            //  JOptionPane.showMessageDialog(null,"Wyjatek SQL - Blad przy zamykaniu bazy.");
        }
}
public boolean wykonaj(String sql){
    try{
        stmt.execute(sql);
        return true;
    }catch(Exception e){
    return false;
    }
}
public String[][] zapytaj(String sql){
   // String[][] tmp;
    try{
   rs = stmt.executeQuery(sql);
   
    rs.last();
   
    String[][] tmp=new String[rs.getRow()][rs.getMetaData().getColumnCount()];
    System.out.println(tmp[0].length);
    System.out.println(tmp.length);
    rs.beforeFirst();
    int i=0;
    int j=0;
    while(rs.next()){
    while(j<rs.getMetaData().getColumnCount()){
        
        tmp[i][j]=rs.getString(j+1);
        j++;
    }
    i++;
    j=0;
    }
    return tmp;
    }catch(SQLException e){
        System.out.println(e.toString());
        System.out.println("..!!..");
       return null;
    }
}
}

